<?
require_once("base/BaseImageForm.php");
require_once("cms/core/om/Image.php");

class ImageForm extends BaseImageForm {

    private $log;
    private $base;

    public function __construct() {
        parent::__construct();
        $this->log =& LoggerManager::getLogger("ImageForm");
        $this->getGalleryImage()->setImageRelatedByImageId(new Image());
        $this->getGalleryImage()->setImageRelatedByThumbnailId(new Image());
    }

    public function reset(ActionConfig $mapping, Request $request) {
        $this->base = $request->getContext()->getRealPath("");
    }

    public function validate() {

        global $ALLOWABLE_IMAGES;

        $ALLOWABLE_IMAGES = array(
            "image/jpeg"=>"jpg",
            "image/pjpeg"=>"jpg",
            "image/gif"=>"gif",
            "image/png"=>"png"
        );

        $errors = parent::validate();

        if($this->getGalleryImage()->getGalleryImageId() == "" &&
            $this->getPath() == "" &&
            (is_null($this->formFile) || !is_array($this->formFile) || $this->formFile["size"] == 0))
                $errors->add("formFile",
                    new ActionMessage("errors.required", true, new ActionMessage("prompt.image", true)));

        if(strlen($this->getPath()) > 0) {
            $path = $this->base . (strpos($this->getPath(), "/") === 0 ? $this->getPath() : "/".$this->getPath());
            if(!is_dir($path))
                $errors->add("path",
                    new ActionMessage("error.path", true, new ActionMessage($path, false)));
        }

        return $errors;
    }

}
?>